package com.three.digit.group;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.three.digit.model.Point;

public abstract class BasicGroup implements IGroup
{
    protected HashMap <String, List <Point>> group;

    public BasicGroup (boolean useBasicData)
    {
        this.group = new HashMap <String, List <Point>> ();

        doGroup (useBasicData ? BASE_LIST : HIS_LIST);
    }

    protected void doGroup (List <Point> basicData)
    {
        for (int i = 0; i < basicData.size (); i++)
        {
            Point point = basicData.get (i);

            String[] hashKey = hashKey (point);
            for (int j = 0; j < hashKey.length; j++)
            {
                List <Point> itemList = group.get (hashKey[j]);
                if (itemList != null)
                {
                    itemList.add (point);
                }
                else
                {
                    itemList = new ArrayList <Point> ();
                    itemList.add (point);
                    group.put (hashKey[j], itemList);
                }
            }
        }
    }

    public List <Point> getGroup (String key)
    {
        return group.get (key);
    }

    public Set <String> keySet ()
    {
        return group.keySet ();
    }

    public abstract String[] hashKey (Point point);
}
